import 'package:base_plugin/base_plugin.dart';
import 'package:dio/dio.dart';

class HttpCoaService {
  /// 获取coa
  static Future<Response> coaList({String as_at}) {
    return BaseConfig.httpBase.get('/api/coa', {
      'as_at': as_at,
    });
  }

  /// 获取新coa
  static Future<Response> coaNewList({String as_at}) {
    return BaseConfig.httpBase.get('/api/coa/coa_index', {
      'as_at': as_at,
    });
  }

  /// 获取最终合成的coa数据
  static Future<Response> getInitCoa() {
    return BaseConfig.httpBase.get('/api/init/coa', {});
  }
  /// 保存coa数据
  static Future<Response> saveCoaData() {
    return BaseConfig.httpBase.get('/api/init/coa/save', {});
  }

  ///  获取coa中inventory对应的数据，通过它的open_balance的值是否为0来判断是否有初始化过
  static Future<Response> hasSetOpenningBalance() {
    return BaseConfig.httpBase.get('/api/coa/inventory', {});
  }

  /// 创建coa
  static Future<Response> createCoa({
    String name,
    int parent_account_id,
    String external_account_code,
    String description,
    String currency_code,
    num open_balance,
    String as_at,
    List map,
  }) {
    return BaseConfig.httpBase.postJson('/api/coa', {
      "name": name,
      "parent_account_id": parent_account_id,
      "external_account_code": external_account_code,
      "description": description,
      "currency_code": currency_code,
      "open_balance": open_balance,
      "as_at": as_at,
      'map': map,
    });
  }

  /// 修改coa
  static Future<Response> updateCoa(
    int id, {
    String name,
    int parent_account_id,
    String external_account_code,
    String description,
    String currency_code,
    num open_balance,
    String as_at,
    List map,
  }) {
    return BaseConfig.httpBase.putJson('/api/coa/$id', {
      "name": name,
      "parent_account_id": parent_account_id,
      "external_account_code": external_account_code,
      "description": description,
      "currency_code": currency_code,
      "open_balance": open_balance,
      "as_at": as_at,
      "map": map,
    });
  }

  /// 删除 coa
  static Future<Response> deleteCoa(int id) {
    return BaseConfig.httpBase.deleteJson('/api/coa/$id', {});
  }

  /// 创建 journal
  static Future<Response> createJournal({
    String date,
    String number,
    String journal_ref_id,
    String currency,
    num amount,
    String memo,
    List journalDetail,
    List attachment_ids,
  }) {
    return BaseConfig.httpBase.postJson('/api/journal', {
      "date": date,
      "number": number,
      "journal_ref_id": journal_ref_id,
      "currency": currency,
      "amount": amount,
      "memo": memo,
      "journalDetail": journalDetail,
      'attachment_ids': attachment_ids ?? [],
    });
  }

  /// 更新 journal
  static Future<Response> updateJournal(
    int id, {
    String date,
    String number,
    String journal_ref_id,
    String currency,
    num amount,
    String memo,
    List journalDetail,
    List attachment_ids,
  }) {
    return BaseConfig.httpBase.putJson('/api/journal/$id', {
      "date": date,
      "number": number,
      "journal_ref_id": journal_ref_id,
      "currency": currency,
      "amount": amount,
      "memo": memo,
      "journalDetail": journalDetail,
      'attachment_ids': attachment_ids ?? [],
    });
  }

  /// 删除 journal
  static Future<Response> deleteJournal(int id) {
    return BaseConfig.httpBase.deleteJson('/api/journal/$id', {});
  }

  /// 删除 journal
  static Future<Response> showJournal(int id) {
    return BaseConfig.httpBase.get('/api/journal/$id', {});
  }

  static Future<Response> lineItems(int id, List map) {
    return BaseConfig.httpBase.postJson('/api/coa/$id/link_items', {
      'map': map,
    });
  }

  /// 设置balances
  static Future<Response> setOpeningBalance(List lines) {
    return BaseConfig.httpBase.postJson('/api/coa/balances', {
      'coalist': lines,
    });
  }
}
